import Vue from 'vue'
import urlConfig from "@/request/urlConfig.js"; // 引入接口地址配置

/**上传图片到后端，返回半路径
 * */
export function uploadImage(url) {
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: urlConfig.baseURL + `/app/upload/public/uploadImage`,
			filePath: url,
			fileType: 'image',
			name: 'file',
			header: {
				'Authorization': uni.getStorageSync('token')
			},
			success: (res) => {
				uni.showLoading({
					title: '图片上传中...',
					mask: true
				})
				resolve(res)
			},
			fail: (req) => {
				reject(req)
			},
			complete: () => {
				uni.hideLoading()
			}
		})
	})
}
/**
 * 时间戳转为年月日
 * */
export function changeDate(timestamp) {
	const date = new Date(timestamp);
	const year = date.getFullYear();
	const month = ('0' + (date.getMonth() + 1)).slice(-2);
	const day = ('0' + date.getDate()).slice(-2);
	return `${year}-${month}-${day}`;
}
export function getCurrentDateTime() {
	let now = new Date()
	// 获取年月日时分秒
	let year = now.getFullYear()
	let month = (now.getMonth() + 1).toString().padStart(2, '0') // 月份从0开始，需要加1，并补零
	let day = now.getDate().toString().padStart(2, '0') // 获取日期，并补零
	let hours = now.getHours().toString().padStart(2, '0') // 获取小时，并补零
	let minutes = now.getMinutes().toString().padStart(2, '0') // 获取分钟，并补零
	let seconds = now.getSeconds().toString().padStart(2, '0') // 获取秒，并补零
	// 构造格式化时间字符串
	let formattedDateTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`

	return formattedDateTime
}
// 挂载到vue中
Vue.prototype.$uploadImage = uploadImage; //上传图片到后端
Vue.prototype.$changeDate = changeDate; //时间戳转为年月日
Vue.prototype.getCurrentDateTime = getCurrentDateTime; //获取当前年月日时分秒